package com.winit.openapi.wh.outbound.validator;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.outbound.vo.OutboundOrderEnumContants;
import com.winit.oms.spi.outbound.vo.OutboundOrderEnumContants.OrderType;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.wh.utils.ValidateUtils;

public class OutboundOrderfieldValidator {

    /**
     * 为了不影响智能仓库推荐使用此方法，此方法保留
     * 
     * @param json
     */
    public static void validOutboundOrder(JSONObject json) {
        validBaseField(json, OrderType.SD.getType());
        validSecondStepBaseField(json);
        validThirdStepBaseField(json);
        validFouthStepBaseField(json);
    }

    /**
     * 校验批量导入入参
     * 
     * @param json
     */
    public static void validImportOutboundOrder(JSONObject json) {
        ValidateUtil.validNotNull(json, "winitProductCode", "Shipping Method");
        ValidateUtil.validMaxLength(json, "winitProductCode", "Shipping Method", 32, true);
        String winitProductCode = json.getString("winitProductCode");
        String orderType = getOrderType(winitProductCode);
        ValidateUtil.validMaxLength(json, "dispatchWhCode", "Warehouse", 40, false);

        ValidateUtil.validNotNull(json, "dispatchWhName", "Warehouse");
        ValidateUtil.validMaxLength(json, "dispatchWhName", "Warehouse", 60, true);

        ValidateUtil.validMaxLength(json, "orderNo", 64, false);

        // 卖家订单号未校验
        ValidateUtil.validMaxLength(json, "customerOrderNo", "Seller Order NO.", 40, false);
        // 是否是重复订单
        ValidateUtil.validMaxLength(json, "isRepeat", "Duplicate order", 10, false);
        // 是否重复订单
        ValidateUtil.validByRegex(json, "isRepeat", "[YN]", "Duplicate order");

        // 标准出库验证
        if (OrderType.SD.getType().equals(orderType)) {
            // 收件人邮编
            ValidateUtil.validMaxLength(json, "buyerPostcode", "Buyer Postcode", 50, true);
            // 收件人国家
            ValidateUtil.validMaxLength(json, "buyerCountry", "Buyer Country", 50, true);
            // 城市
            ValidateUtil.validMaxLength(json, "buyerCity", "Buyer City", 50, true);
            // 州、省
            ValidateUtil.validMaxLength(json, "buyerState", "Buyer State", 50, false);
            
            // 是否是ebay订单校验不能为空
            ValidateUtil.validMaxLength(json, "isEbay", 10, true);
            // 是否是ebay订单
            ValidateUtil.validByRegex(json, "isEbay", "[YN]");

            if ("Y".equals(json.getString("isEbay"))) {
                ValidateUtil.validMaxLength(json, "ebaySellerId", "eBaySellerID", 30, false);
                ValidateUtil.validMaxLength(json, "ebaybuyerId", "eBayBuyerID", 30, false);
            }
            ValidateUtil.validNotNull(json, "buyerName", "Buyer Fullname");
            ValidateUtil.validMaxLength(json, "buyerName", 60, true);

            ValidateUtil.validMaxLength(json, "buyerHouseNo", "House No.", 60, false);

            ValidateUtil.validNotNull(json, "buyerAddress1", "Buyer Address 1");
            ValidateUtil.validMaxLength(json, "buyerAddress1", 100, true);

            ValidateUtil.validMaxLength(json, "buyerAddress2", "Buyer Address 2", 100, false);

//            ValidateUtil.validNotNull(json, "buyerPhone", "Buyer Phone Number");
            ValidateUtil.validMaxLength(json, "buyerPhone", "Buyer Phone Number", 20, false);

            ValidateUtil.validMaxLength(json, "buyerEmail", "Buyer Email", 50, false);
        } else if (OrderType.SP.getType().equals(orderType)) {
            // 取件人姓名
            ValidateUtil.validNotNull(json, "buyerName", "Buyer Fullname");
            ValidateUtil.validMaxLength(json, "buyerName", "Buyer Fullname", 60, true);
            // 取件人电话
            ValidateUtil.validMaxLength(json, "buyerPhone", "Buyer Phone Number", 20, false);
        } else if (OrderType.DS.getType().equals(orderType)) {

        } else {
            // 抛异常：{字段[{}]:不支持的WINIT产品编码}
            throw new ApiException(ErrorCode.UNSUPPORT_WINIT_PRODUCT, "Shipping Method", winitProductCode);
        }

        JSONArray array = json.getJSONArray("merchandiseInfoList");
        if (null == array || array.size() <= 0) {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "merchandiseInfoList");
        }
        for (Object item : array) {
            JSONObject obj = (JSONObject) item;
            ValidateUtil.validNotNull(obj, "merchandiseCode", "SKU No.");
            ValidateUtil.validMaxLength(obj, "merchandiseCode", "SKU No.", 64, true);

            ValidateUtil.validMaxLength(obj, "specification", "Attribute", 64, false);

            ValidateUtil.validNotNull(obj, "qty", "Quantity");
            ValidateUtil.validMaxLength(obj, "qty", "Quantity", 16, true);
            ValidateUtils.isNumber(obj.getString("qty"), "qty"); // 校验必须是数字
            if ("Y".equals(json.getString("isEbay"))) {
                ValidateUtil.validNotNull(obj, "ebayTransactionId", "Transaction ID");
                ValidateUtil.validMaxLength(obj, "ebayTransactionId", "Transaction ID", 64, true);

                ValidateUtil.validNotNull(obj, "ebayItemId", "Item Number");
                ValidateUtil.validMaxLength(obj, "ebayItemId", "Item Number", 64, true);
            }
        }
        //增值服务
        JSONArray vasarray = json.getJSONArray("VASList");
        if (null != vasarray && vasarray.size() > 0) {
            Map<String, Object> map = new HashMap<String, Object>();
            for (Object item : vasarray) {
                JSONObject obj = (JSONObject) item;
                ValidateUtil.validNotNull(obj, "serviceCode", "Value-added service");
                ValidateUtil.validMaxLength(obj, "serviceCode", 32, true);
                String serviceCode = obj.getString("serviceCode");
                if (map != null && map.containsKey(serviceCode)) {
                    throw new ApiException(ErrorCode.VAS_CANNOT_BE_REPEAT,serviceCode);
                }
                map.put(serviceCode, obj);
                JSONArray attributeArray = obj.getJSONArray("attrList");
                if (null == attributeArray || attributeArray.size() == 0) {
                    continue;
                }
                for (Object attribute : attributeArray) {
                    JSONObject attributeobj = (JSONObject) attribute;
                    ValidateUtil.validNotNull(attributeobj, "attrCode", "Insured amount");
                    ValidateUtil.validMaxLength(attributeobj, "attrCode", "Insured amount", 128, false);

                    ValidateUtil.validMaxLength(attributeobj, "attrValue", "Insured amount", 128, false);
                    ValidateUtil.validBigDecimal(attributeobj, "attrValue", "Insured amount", false);
                    ValidateUtil.validatePrecision(attributeobj,"attrValue","Insured amount",6);
                }
            }
        }
    }

    /**
     * 校验再次批量导入入参
     * 
     * @param json
     */
    public static void validUpdateOutboundOrder(JSONObject json) {
        ValidateUtil.isNotNull(json, "orderNo");
        ValidateUtil.validMaxLength(json, "orderNo", 64, true);
        validImportOutboundOrder(json);
    }

    /**
     * 校验一步下单入参
     * 
     * @param json
     */
    public static void validCreateOutboundOrder(JSONObject json) {
        String winitProductCode = json.getString("winitProductCode");
        String orderType = getOrderType(winitProductCode);
        validCreateBaseField(json, orderType);
        validCreateSecondStepBaseField(json);
        validCreateThirdStepBaseField(json);
        if (OrderType.SD.getType().equals(orderType)) {
            validCreateFouthStepBaseField(json);
        }
        //校验增值服务
        validVas(json);
    }
    
    /**
     * 校验增值服务字段
     * @param json
     */
    public static void validVas(JSONObject json) {
        JSONArray array = json.getJSONArray("VASList");
        if (null != array && array.size() > 0) {
            Map<String, Object> map = new HashMap<String, Object>();
            for (Object item : array) {
                JSONObject obj = (JSONObject) item;
                ValidateUtil.validNotNull(obj, "serviceCode");
                ValidateUtil.validMaxLength(obj, "serviceCode", 32, true);
                String serviceCode = obj.getString("serviceCode");
                if (map.containsKey(serviceCode)) {
                    throw new ApiException(ErrorCode.VAS_CANNOT_BE_REPEAT, serviceCode);
                }
                map.put(serviceCode, serviceCode);
                JSONArray attributeArray = obj.getJSONArray("attrList");
                if (null == attributeArray || attributeArray.size() == 0) {
                    continue;
                }
                Map<String, Object> attrMap = new HashMap<String, Object>();
                for (Object attribute : attributeArray) {
                    JSONObject attributeobj = (JSONObject) attribute;
                    ValidateUtil.validNotNull(attributeobj, "attrCode");
                    ValidateUtil.validMaxLength(attributeobj, "attrCode", 128, false);
                    String attrCode = attributeobj.getString("attrCode");
                    if (attrMap.containsKey(attrCode)) {
                        throw new ApiException(ErrorCode.VAS_ATTRIBUTE_CANNOT_BE_REPEAT, serviceCode, attrCode);
                    }
                    attrMap.put(attrCode, attrCode);
                    ValidateUtil.validNotNull(attributeobj, "attrValue");
                    ValidateUtil.validMaxLength(attributeobj, "attrValue", 128, false);
                    ValidateUtil.validBigDecimal(attributeobj, "attrValue");
                    ValidateUtil.validatePrecision(attributeobj, "attrValue", "attrValue", 6);
                }
            }
        }
    }

    /**
     * 通过产品编码获得订单类型
     * 
     * @param winitProductCode 产品编码
     * @return
     */
    public static String getOrderType(String winitProductCode) {
        String orderType = null;
        if (winitProductCode.startsWith(OrderType.SD.getType())) {
            orderType = OrderType.SD.getType();
        } else if (winitProductCode.startsWith(OrderType.SP.getType())) {
            orderType = OrderType.SP.getType();
        } else if (winitProductCode.startsWith(OrderType.DS.getType())) {
            orderType = OrderType.DS.getType();
        }
        return orderType;
    }

    /**
     * 构建共用参数
     * 
     * @param json
     */
    public static void validBaseField(JSONObject json, String orderType) {

        ValidateUtil.validMaxLength(json, "dispatchWhCode", 40, true);
        ValidateUtil.validMaxLength(json, "dispatchWhName", 60, false);
        ValidateUtil.validMaxLength(json, "orderNo", 64, false);
        ValidateUtil.validMaxLength(json, "isRepeat", 10, false);//校验订单是否重复字段
        ValidateUtil.validByRegex(json, "isRepeat", "[YN]");//校验订单时候重复只能填Y/N
        ValidateUtil.validMaxLength(json, "customerOrderNo", 40, false);//校验客户订单
        // 标准出库验证
        if (OrderType.SD.getType().equals(orderType)) {
            // 收件人邮编
            ValidateUtil.validMaxLength(json, "buyerPostcode", 50, true);
            // 收件人国家
            ValidateUtil.validMaxLength(json, "buyerCountry", 50, true);
            // 城市
            ValidateUtil.validMaxLength(json, "buyerCity", 50, true);
            // 州、省
            // ValidateUtil.validMaxLength(json, "buyerState", 50, true);
            // 是否是ebay订单校验不能为空
            ValidateUtil.validMaxLength(json, "isEbay", 10, true);
            // 是否是ebay订单
            ValidateUtil.validByRegex(json, "isEbay", "[YN]");

            if ("Y".equals(json.getString("isEbay"))) {
                ValidateUtil.validMaxLength(json, "ebaySellerId", 30, false);
                ValidateUtil.validMaxLength(json, "ebaybuyerId", 30, false);
            }
        } else if (OrderType.SP.getType().equals(orderType)) {
            // 取件人姓名
            ValidateUtil.validMaxLength(json, "buyerName", 60, true);
            // 取件人电话
            ValidateUtil.validMaxLength(json, "buyerPhone", 20, false);
            // 取件时间
            ValidateUtil.validMaxLength(json, "takeTime", 128, true);
            
        } else if (OrderType.DS.getType().equals(orderType)) {
            
        } else {
            // 抛异常：{不支持的订单类型}
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, orderType);
        }
    }

    /**
     * 构建共用参数
     * 
     * @param json
     */
    public static void validCreateBaseField(JSONObject json, String orderType) {

        ValidateUtil.validMaxLength(json, "warehouseCode", 40, true);
        ValidateUtil.validMaxLength(json, "orderNo", 64, false);
        // 卖家订单号未校验
        ValidateUtil.validMaxLength(json, "shipperOrderNo", 40, false);
        // 是否是重复订不能为空
        ValidateUtil.validMaxLength(json, "isRepeat", 1, true);
        // 是否重复订单传值只能是Y/N
        ValidateUtil.validByRegex(json, "isRepeat", "[YN]");
        // 平台名称
        ValidateUtil.validMaxLength(json,"storeType",100,false);
        // 标准出库验证
        if (OrderType.SD.getType().equals(orderType)) {
            // 收件人邮编
            ValidateUtil.validMaxLength(json, "receiverPostcode", 50, true);
            // 收件人国家
            ValidateUtil.validMaxLength(json, "receiverCountry", 50, true);
            // 城市
            ValidateUtil.validMaxLength(json, "receiverCity", 50, true);
            // 州、省
            ValidateUtil.validMaxLength(json, "receiverState", 50, false);
            // 是否是ebay订单长度校验
            ValidateUtil.validMaxLength(json, "isEbay", 1, false);
            // 是否是ebay订单传值只能是Y/N
            ValidateUtil.validByRegex(json, "isEbay", "[YN]");
            // 老系统支持50
            ValidateUtil.validMaxLength(json, "eBaySellerId", 50, false);
            ValidateUtil.validMaxLength(json, "eBayBuyerId", 50, false);
        } else if (OrderType.SP.getType().equals(orderType)) {
            // 收货人姓名
            ValidateUtil.validMaxLength(json, "receiverName", 60, true);
            // 收货人电话
            ValidateUtil.validMaxLength(json, "receiverPhone", 20, true);
        } else if (OrderType.DS.getType().equals(orderType)) {
            
        } else {
            // 抛异常：{不支持的订单类型}
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, orderType);
        }
    }

    /**
     * 校验创建海外仓出库单参数 第一步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveFirstStep
     * 
     * @param json
     */
    public static void validFirstStepField(JSONObject json) {
        ValidateUtil.validNotNull(json, "orderType");
        String orderType = json.getString("orderType");
        validBaseField(json, orderType);
    }

    /**
     * 校验创建海外仓出库单参数 第二步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveSecondStep
     * 
     * @param json
     */
    public static void validSecondStepField(JSONObject json) {
        ValidateUtil.isNotNull(json, "orderNo");
        ValidateUtil.validMaxLength(json, "orderNo", 64, true);
        // 是否跨仓发货
        ValidateUtil.validMaxLength(json, "isAcrossWarehouse", 1, false);
        ValidateUtil.validByRegex(json, "isAcrossWarehouse", "[YN]");
        validSecondStepBaseField(json);
    }

    /**
     * 校验创建海外仓出库单参数 第二步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveSecondStep
     * 
     * @param json
     */
    public static void validSecondStepBaseField(JSONObject json) {
        JSONArray array = json.getJSONArray("merchandiseInfoList");

        if (null == array || array.size() <= 0) {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "merchandiseInfoList");
        }
        for (Object item : array) {
            JSONObject obj = (JSONObject) item;
            ValidateUtil.validNotNull(obj, "merchandiseCode");
            ValidateUtil.validMaxLength(obj, "merchandiseCode", 64, true);

            ValidateUtil.validMaxLength(obj, "specification", 64, false);

            ValidateUtil.validNotNull(obj, "qty");
            ValidateUtil.validMaxLength(obj, "qty", 16, true);
            ValidateUtils.isNumber(obj.getString("qty"), "qty"); // 校验必须是数字
            if ("Y".equals(json.getString("isEbay"))) {
                ValidateUtil.validMaxLength(obj, "ebayTransactionId", 64, true);
                ValidateUtil.validMaxLength(obj, "ebayItemId", 64, true);
            }
        }
    }

    /**
     * 校验创建海外仓出库单参数 第二步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveSecondStep
     * 
     * @param json
     */
    public static void validCreateSecondStepBaseField(JSONObject json) {
        JSONArray array = json.getJSONArray("merchandiseList");

        if (null == array || array.size() <= 0) {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "merchandiseList");
        }
        for (Object item : array) {
            JSONObject obj = (JSONObject) item;
            ValidateUtil.validMaxLength(obj, "merchandiseCode", 64, true);
            ValidateUtil.validMaxLength(obj, "specification", 64, false);
            ValidateUtil.validMaxLength(obj, "quantity", 16, true);
            ValidateUtil.validInteger(obj, "quantity", true);// 需要校验必须为数字
            ValidateUtil.validMaxLength(obj, "eBayTransactionId", 64, false);
            ValidateUtil.validMaxLength(obj, "eBayItemId", 64, false);
        }
    }

    /**
     * 校验创建海外仓出库单参数 第三步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveThirdStep
     * 
     * @param json
     */
    public static void validThirdStepField(JSONObject json) {
        ValidateUtil.validMaxLength(json, "orderNo", 64, true);
        validThirdStepBaseField(json);

    }

    /**
     * 校验第三步入参
     * 
     * @param json
     */
    public static void validThirdStepBaseField(JSONObject json) {
        ValidateUtil.validMaxLength(json, "winitProductCode", 32, true);
        ValidateUtil.validMaxLength(json, "invoiceUrl", 256, false);
        /*
         * if (json.containsKey("takeStartTime")) {// 取件开始时间
         * ValidateUtil.validMaxLength(json, "takeStartTime", 60, true); } if
         * (json.containsKey("takeEndTime")) {// 取件结束时间
         * ValidateUtil.validMaxLength(json, "takeEndTime", 60, true); } if
         * (json.containsKey("takeStartTime") &&
         * json.containsKey("takeEndTime")) { ValidateUtil.validDateBefore(json,
         * "takeStartTime", "takeEndTime"); }
         */
        //校验增值服务
        validVas(json);
    }

    public static void validCreateThirdStepBaseField(JSONObject json) {
        ValidateUtil.validMaxLength(json, "winitProductCode", 32, true);
        ValidateUtil.validMaxLength(json, "invoiceUrl", 256, false);
    }

    /**
     * 校验创建海外仓出库单参数 第四步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveFouthStep
     * 
     * @param json
     */
    public static void validFouthStepBaseField(JSONObject json) {
        ValidateUtil.validMaxLength(json, "buyerName", 60, true);
        ValidateUtil.validMaxLength(json, "buyerHouseNo", 60, false);
        ValidateUtil.validMaxLength(json, "buyerAddress1", 100, true);
        ValidateUtil.validMaxLength(json, "buyerAddress2", 100, false);
        ValidateUtil.validMaxLength(json, "buyerPhone", 20, false);
        ValidateUtil.validMaxLength(json, "buyerEmail", 50, false);
    }

    /**
     * 校验创建海外仓出库单参数 第四步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveFouthStep
     * 
     * @param json
     */
    public static void validCreateFouthStepBaseField(JSONObject json) {
        ValidateUtil.validMaxLength(json, "receiverName", 60, true);
        ValidateUtil.validMaxLength(json, "receiverHouseNo", 60, false);
        ValidateUtil.validMaxLength(json, "receiverAddressLine1", 100, true);
        ValidateUtil.validMaxLength(json, "receiverAddressLine2", 100, false);
        ValidateUtil.validMaxLength(json, "receiverPhone", 20, false);
        ValidateUtil.validMaxLength(json, "receiverEmail", 50, false);
    }

    /**
     * 校验创建海外仓出库单参数 第四步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveFouthStep
     * 
     * @param json
     */
    public static void validFouthStepField(JSONObject json) {
        ValidateUtil.validMaxLength(json, "orderNo", 64, true);
        validFouthStepBaseField(json);
    }

    /**
     * 校验单步提交订单入参
     * 
     * @param json
     */
    public static void validSubmitField(JSONObject json) {
        ValidateUtil.isNotNull("orderNo");
        ValidateUtil.validMaxLength(json, "orderNo", 64, true);

        ValidateUtil.isNotNull(json, "orderType");
        String orderType = json.getString("orderType");
        boolean type = OutboundOrderEnumContants.OrderType.DS.getType().equals(orderType)
                       || OutboundOrderEnumContants.OrderType.SP.getType().equals(orderType);
        if (type) {
            ValidateUtil.isNotNull(json, "winitProductCode");
            ValidateUtil.validMaxLength(json, "winitProductCode", 32, true);
        } else if (OutboundOrderEnumContants.OrderType.SD.getType().equals(orderType)) {
            ValidateUtil.validMaxLength(json, "buyerName", 60, true);
            ValidateUtil.validMaxLength(json, "buyerHouseNo", 60, false);
            ValidateUtil.validMaxLength(json, "buyerAddress1", 100, true);
            ValidateUtil.validMaxLength(json, "buyerAddress2", 100, false);
            ValidateUtil.validMaxLength(json, "buyerPhone", 20, false);
            ValidateUtil.validMaxLength(json, "buyerEmail", 50, false);
        }
    }

    /**
     * 出库单列表查询校验.
     * 
     * @param json
     */
    public static void validQueryByMainOrderParam(JSONObject json) {

        if (json.containsKey("orderNo")) {// 订单号
            ValidateUtil.validMaxLength(json, "orderNo", 64);
        }
        if (json.containsKey("winitTrackingNo")) {// winit跟踪号
            ValidateUtil.validMaxLength(json, "winitTrackingNo", 64);
        }
        if (json.containsKey("sellerOrderNo")) {// 卖家订单号
            ValidateUtil.validMaxLength(json, "sellerOrderNo", 40);
        }
        if (json.containsKey("orderStatus")) {// 订单状态
            ValidateUtil.validMaxLength(json, "orderStatus", 30);
        }
        if (json.containsKey("buyerName")) {// 收件人姓名
            ValidateUtil.validMaxLength(json, "buyerName", 60);
        }
        if (json.containsKey("dispatchWhCode")) {// 发货仓编号
            ValidateUtil.validMaxLength(json, "dispatchWhCode", 40);
        }
        if (json.containsKey("orderType")) {// 订单类型
            ValidateUtil.validMaxLength(json, "orderType", 30);
        }
        if (json.containsKey("trackingNo")) {// 快递单号
            ValidateUtil.validMaxLength(json, "trackingNo", 32);
        }
        if (json.containsKey("merchandiseCode")) {// 商品编码
            ValidateUtil.validMaxLength(json, "merchandiseCode", 255);
        }
        if (json.containsKey("merchandiseName")) {// 商品名称
            ValidateUtil.validMaxLength(json, "merchandiseName", 255);
        }
        if (json.containsKey("orderSource")) {// 订单来源
            ValidateUtil.validMaxLength(json, "orderSource", 10);
        }
        if (json.containsKey("customerName")) {// 下单人
            ValidateUtil.validMaxLength(json, "customerName", 60);
        }
        if (json.containsKey("validateResult")) {// 校验结果（Y或者N）
            ValidateUtil.validMaxLength(json, "validateResult", 1);
        }
        if (json.containsKey("isEbay")) {// 是否是ebay订单
            ValidateUtil.validMaxLength(json, "isEbay", 1);
        }
        if (json.containsKey("eGDValidateCode")) {// egd校验编码
            ValidateUtil.validMaxLength(json, "eGDValidateCode", 16);
        }
        if (json.containsKey("isAcrossWarehouse")) {
            ValidateUtil.validMaxLength(json, "isAcrossWarehouse", 1);
        }
        if (json.containsKey("ebaySellerID")){ // 卖家ebayId
            ValidateUtil.validMaxLength(json, "ebaySellerID",128);
        }
    }

    /**
     * 校验出库单修改信息.
     * 
     * @param json
     */
    public static void validateModifyOutboundOrder(JSONObject json) {
        JSONObject obj = json.getJSONObject("buyerInfoVo");
        ValidateUtil.validNotNull(json, "orderNo");
        ValidateUtil.validNotNull(obj, "buyerName");
        ValidateUtil.validNotNull(obj, "buyerPhone");
        ValidateUtil.validNotNull(obj, "buyerCountry");
        ValidateUtil.validNotNull(obj, "buyerCity");
        ValidateUtil.validNotNull(obj, "buyerPostcode");
        ValidateUtil.validNotNull(obj, "buyerAddress1");
        ValidateUtil.validMaxLength(obj, "orderNo", 64);
        ValidateUtil.validMaxLength(obj, "buyerName", 60);
        ValidateUtil.validMaxLength(obj, "buyerEmail", 50);
        ValidateUtil.validMaxLength(obj, "buyerPhone", 20);
        ValidateUtil.validMaxLength(obj, "buyerHouseNo", 60);
        ValidateUtil.validMaxLength(obj, "buyerCountry", 50);
        ValidateUtil.validMaxLength(obj, "buyerState", 50);
        ValidateUtil.validMaxLength(obj, "buyerCity", 50);
        ValidateUtil.validMaxLength(obj, "buyerPostcode", 50);
        ValidateUtil.validMaxLength(obj, "buyerAddress1", 100);
        ValidateUtil.validMaxLength(obj, "buyerAddress2", 100);
    }

    /**
     * 校验订单入参.
     * 
     * @param json
     */
    public static void validreOrderField(JSONObject json) {
        if (json.containsKey("orderNo")) {// 订单号
            ValidateUtil.validNotNull(json, "orderNo");
            ValidateUtil.validMaxLength(json, "orderNo", 64);
        }
    }

    
    /**
     * 校验计价参数信息.
     * 
     * @param json
     */
    public static void validParam(JSONObject json) {
        if (json.containsKey("warehouseCode")) {
            ValidateUtil.validNotNull(json, "warehouseCode");
            ValidateUtil.validMaxLength(json, "warehouseCode", 128);
        }
        if (json.containsKey("countryCode")) {
            ValidateUtil.validNotNull(json, "countryCode");
            ValidateUtil.validMaxLength(json, "countryCode", 128);
        }
        if (json.containsKey("destinationPostcode")) {
            ValidateUtil.validNotNull(json, "destinationPostcode");
            ValidateUtil.validMaxLength(json, "destinationPostcode", 16);
        }
        if (json.containsKey("length")) {
            ValidateUtil.validBigDecimal(json, "length", true);
            ValidateUtil.validatePrecision(json, "length", 16, 2);
        }
        if (json.containsKey("width")) {
            ValidateUtil.validBigDecimal(json, "width", true);
            ValidateUtil.validatePrecision(json, "width", 16, 2);
        }
        ValidateUtil.validBigDecimal(json, "height", true);
        if (json.containsKey("height")) {
            ValidateUtil.validatePrecision(json, "height", 16, 2);
        }
        if (json.containsKey("weight")) {
            ValidateUtil.validBigDecimal(json, "weight", true);
            ValidateUtil.validatePrecision(json, "weight", 16, 4);
        }
    }
    /**
     * 校验计价参数信息.
     * 
     * @param json
     */
    public static void newValidParam(JSONObject json) {
        ValidateUtil.validNotNull(json, "country"); 
        ValidateUtil.validMaxLength(json, "country", 128);
        JSONArray array = json.getJSONArray("productList");
        for (Object item : array) {
            JSONObject obj = (JSONObject) item;
            if (null != obj.get("productCode") && StringUtils.isNotBlank(obj.get("productCode").toString())) {
                // 规格
                if (obj.containsKey("specification")) {
                    ValidateUtil.validMaxLength(obj, "specification", 255);
                }
            } else {
                // 不存在商品编码 那商品尺寸必填
                ValidateUtil.validBigDecimal(obj, "length", true);
                ValidateUtil.validNotZreo(json, "length");
                ValidateUtil.validatePrecision(obj, "length", 16, 2);

                ValidateUtil.validBigDecimal(obj, "width", true);
                ValidateUtil.validNotZreo(json, "width");
                ValidateUtil.validatePrecision(obj, "width", 16, 2);

                ValidateUtil.validBigDecimal(obj, "height", true);
                ValidateUtil.validNotZreo(json, "height");
                ValidateUtil.validatePrecision(obj, "height", 16, 2);

                ValidateUtil.validBigDecimal(obj, "weight", true);
                ValidateUtil.validNotZreo(json, "weight");
                ValidateUtil.validatePrecision(obj, "weight", 16, 4);
            }
            if (null != obj.get("productNum")&& StringUtils.isNotBlank(obj.get("productNum").toString())){  
                ValidateUtil.validInteger(obj, "productNum", true);
                ValidateUtil.validNotZreo(json, "productNum");
                ValidateUtil.validMaxLength(obj, "productNum", 16);
            }
        }
        ValidateUtil.validNotNull(json, "warehouseCode");
        ValidateUtil.validMaxLength(json, "warehouseCode", 40);

        ValidateUtil.validNotNull(json, "zipCode");
        ValidateUtil.validMaxLength(json, "zipCode", 50);
    }

    /**
     * 校验返利传入参数信息.
     * 
     * @param json
     */

    public static void validRebate(JSONObject json) {
        if (json.containsKey("orderNo")) {// 订单号
            ValidateUtil.validMaxLength(json, "orderNo", 60);
        }
        if (json.containsKey("orderType")) {// 订单号
            ValidateUtil.validMaxLength(json, "orderType", 8);
        }
        if (json.containsKey("sellerOrderNo")) {// 卖家订单号
            ValidateUtil.validMaxLength(json, "sellerOrderNo", 40);
        }
        if (json.containsKey("warehouseCode")) {// 倉庫名稱
            ValidateUtil.validMaxLength(json, "warehouseCode", 40);
        }
        if (json.containsKey("buyerName")) {// 收件人姓名
            ValidateUtil.validMaxLength(json, "buyerName", 60);
        }
        if (json.containsKey("orderDateFrom")) {
            ValidateUtil.validMaxLength(json, "orderDateFrom", 60);
        }
        if (json.containsKey("orderDateFromTo")) {
            ValidateUtil.validMaxLength(json, "orderDateFromTo ", 60);
        }
        if (json.containsKey("rebateStatus")) {// 返利状态
            ValidateUtil.validMaxLength(json, "rebateStatus", 60);
        }
    }

    /**
     * 校验返利导入传入参数信息.
     * 
     * @param json
     */

    public static void validImportRebate(JSONObject json) {
        ValidateUtil.validMaxLength(json, "sellerId", 128);
        
        ValidateUtil.validNotNull(json, "ebayTransactionId");
        ValidateUtil.validMaxLength(json, "ebayTransactionId", 128);
        
        ValidateUtil.validNotNull(json, "ebayItemId");
        ValidateUtil.validMaxLength(json, "ebayItemId", 128);
        
        ValidateUtil.validMaxLength(json, "buyerId", 128);
        // 出库单编号
        ValidateUtil.validNotNull(json, "orderNo");
        ValidateUtil.validMaxLength(json, "orderNo ", 64);
    }

    /**
     * 校验单步更新草稿订单
     * 
     * @param json
     */
    public static void validUpdateDraftOrder(JSONObject json) {
        ValidateUtil.isNotNull(json, "orderNo");
        ValidateUtil.validMaxLength(json, "orderNo", 64, true);

        ValidateUtil.validNotNull(json, "winitProductCode");
        ValidateUtil.validMaxLength(json, "winitProductCode", 40, true);
        String winitProductCode = json.getString("winitProductCode");
        String orderType = getOrderType(winitProductCode);
        // 标准出库验证
        if (OrderType.SD.getType().equals(orderType)) {
            ValidateUtil.validMaxLength(json, "sellerOrderNo", 40, false);
            // 是否是重复订不能为空
            ValidateUtil.validMaxLength(json, "isRepeat", 1, true);
            // 是否重复订单传值只能是Y/N
            ValidateUtil.validByRegex(json, "isRepeat", "[YN]");

            ValidateUtil.validNotNull(json, "buyerPostcode");
            ValidateUtil.validMaxLength(json, "buyerPostcode", 50, true);

            ValidateUtil.validNotNull(json, "buyerCountry");
            ValidateUtil.validMaxLength(json, "buyerCountry", 50, true);

            ValidateUtil.validNotNull(json, "buyerCity");
            ValidateUtil.validMaxLength(json, "buyerCity", 50, true);

            // ValidateUtil.validNotNull(json, "buyerState");
            ValidateUtil.validMaxLength(json, "buyerState", 50, false);

            ValidateUtil.validNotNull(json, "buyerName");
            ValidateUtil.validMaxLength(json, "buyerName", 60, true);

            ValidateUtil.validMaxLength(json, "buyerHouseNo", 60, false);

            ValidateUtil.validNotNull(json, "buyerAddress1");
            ValidateUtil.validMaxLength(json, "buyerAddress1", 100, true);

            ValidateUtil.validMaxLength(json, "buyerAddress2", 100, false);
            ValidateUtil.validMaxLength(json, "buyerPhone", 20, false);
            ValidateUtil.validMaxLength(json, "buyerEmail", 50, false);
        }
    }

    public static void validCalcDeliveryParam(JSONObject json) {
        ValidateUtil.validNotNull(json, "country"); 
        ValidateUtil.validMaxLength(json, "country", 128);
        // 9534 优化：海外仓出库费用预估接口，优化city为非必填写项，方便客户使用邮编即可计算费用
        // ValidateUtil.validNotNull(json, "city");
        ValidateUtil.validMaxLength(json, "city", 30);
        JSONArray array = json.getJSONArray("productList");
        for (Object item : array) {
            JSONObject obj = (JSONObject) item;
            if (null != obj.get("productCode")) {  
                ValidateUtil.validNotNull(obj, "productCode");
                ValidateUtil.validMaxLength(obj, "productCode", 100);
                // 规格
                if (obj.containsKey("specification")) {
                    ValidateUtil.validMaxLength(obj, "specification", 255);
                }
            } else {
                // 不存在商品编码 那商品尺寸必填
                ValidateUtil.validBigDecimal(obj, "length", true);
                ValidateUtil.validNotZreo(obj, "length");
                ValidateUtil.validatePrecision(obj, "length", 16, 2);

                ValidateUtil.validBigDecimal(obj, "width", true);
                ValidateUtil.validNotZreo(obj, "width");
                ValidateUtil.validatePrecision(obj, "width", 16, 2);

                ValidateUtil.validBigDecimal(obj, "height", true);
                ValidateUtil.validNotZreo(obj, "height");
                ValidateUtil.validatePrecision(obj, "height", 16, 2);

                ValidateUtil.validBigDecimal(obj, "weight", true);
                ValidateUtil.validNotZreo(obj, "weight");
                ValidateUtil.validatePrecision(obj, "weight", 16, 4);
            }
            if (null != obj.get("productNum")&& StringUtils.isNotBlank(obj.get("productNum").toString())){  
                ValidateUtil.validInteger(obj, "productNum", true);
                ValidateUtil.validNotZreo(obj, "productNum");
                ValidateUtil.validMaxLength(obj, "productNum", 16);
            }
        }
        ValidateUtil.validNotNull(json, "warehouseCode");
        ValidateUtil.validMaxLength(json, "warehouseCode", 40);

        ValidateUtil.validNotNull(json, "zipCode");
        ValidateUtil.validMaxLength(json, "zipCode", 50);
        
        if (json.containsKey("VASList")) {
            JSONArray vasArray = json.getJSONArray("VASList");
            for (Object vas : vasArray) {
                JSONObject obj = (JSONObject) vas;
                ValidateUtil.validNotNull(obj, "serviceCode");
                ValidateUtil.validNotNull(obj, "attrList");
                ValidateUtil.validList(obj, "attrList", true);
                JSONArray vasAttrArray = obj.getJSONArray("attrList");
                for (Object attr : vasAttrArray) {
                    JSONObject attrObj = (JSONObject) attr;
                    ValidateUtil.validNotNull(attrObj, "attrCode");
                    ValidateUtil.validNotNull(attrObj, "attrValue");
                }
            }
        }
    }
    
    /**
     * 尾程派送费用计算器入参校验
     * @param json
     */
    public static void validCalWHDeliveryParam(JSONObject json) {
        ValidateUtil.validNotNull(json, "country"); 
        ValidateUtil.validMaxLength(json, "country", 128);
        
        ValidateUtil.validNotNull(json, "warehouseCode");
        ValidateUtil.validMaxLength(json, "warehouseCode", 40);

        ValidateUtil.validNotNull(json, "zipCode");
        ValidateUtil.validMaxLength(json, "zipCode", 50);
        JSONArray array = json.getJSONArray("merchandiseList");
        for (Object item : array) {
            JSONObject obj = (JSONObject) item;
            if (null != obj.get("merchandiseCode")) {  
                ValidateUtil.validNotNull(obj, "merchandiseCode");
                ValidateUtil.validMaxLength(obj, "merchandiseCode", 100);
                // 规格
                if (obj.containsKey("specification")) {
                    ValidateUtil.validMaxLength(obj, "specification", 255);
                }
                ValidateUtil.validNotNull(obj, "qty");
                ValidateUtil.validInteger(obj, "qty", true);
                ValidateUtil.validNotZreo(obj, "qty");
                ValidateUtil.validMaxLength(obj, "qty", 16);
            } else {
                // 不存在商品编码 那商品尺寸必填
                ValidateUtil.validBigDecimal(obj, "length", true);
                ValidateUtil.validNotZreo(obj, "length");
                ValidateUtil.validatePrecision(obj, "length", 16, 2);

                ValidateUtil.validBigDecimal(obj, "width", true);
                ValidateUtil.validNotZreo(obj, "width");
                ValidateUtil.validatePrecision(obj, "width", 16, 2);

                ValidateUtil.validBigDecimal(obj, "height", true);
                ValidateUtil.validNotZreo(obj, "height");
                ValidateUtil.validatePrecision(obj, "height", 16, 2);

                ValidateUtil.validBigDecimal(obj, "weight", true);
                ValidateUtil.validNotZreo(obj, "weight");
                ValidateUtil.validatePrecision(obj, "weight", 16, 4);
            }
        }
    }
}
